import { Button, InputNumber } from 'antd';

// import { renderOverflowText, getSorter, ColumnsRenderFun, DictionaryRenderFun } from '../common/Format';

import { getColorTextLoadMap, mergeCells } from '@/utils';
import MyIcon from '@/components/MyIcon';
import { DeleteOutlined, PlusCircleOutlined } from '@ant-design/icons';
import { ColumnsRenderFun, DictionaryRenderFun, formatDecimal, getSorter } from '../../Acommon';

/**
 * @getPackTableColumns 采购申请第二步表格配置
 * @param onRemove 删除标的
 * @param changeTargetInfo 选择标的
 * @returns
 */
export const getPackTableColumns = ({ onRemove, changeTargetInfo, finallyType }: Record<string, any>): Record<string, any>[] => {
  let colums = [
    {
      title: '标的名称',
      dataIndex: 'fullName',
      key: 'fullName',
      width: 130,
      visible: true,
      fixed: 'left',
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '采购类别',
      dataIndex: 'supplyTypeName',
      key: 'supplyTypeName',
      width: 150,
      visible: true,
      render: (text: string, record) => DictionaryRenderFun({ text }),
    },
    {
      title: '规格型号（参数）',
      dataIndex: 'needParams',
      key: 'needParams',
      width: 180,
      visible: true,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '物料类型',
      dataIndex: 'supplyKindName',
      key: 'supplyKindName',
      width: 150,
      visible: true,
      render: (text: string, record) => DictionaryRenderFun({ text }),
    },
    {
      title: '数量（单位）',
      dataIndex: 'expectNum',
      key: 'expectNum',
      width: 140,
      visible: true,
      render: (text: string, record: Record<string, any>) => {
        return (
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
            <span>{text}</span>
            <span>{record.unitBusiness ?? ''}</span>
          </div>
        );
      },
    },
    {
      title: '预估单价（元）',
      dataIndex: 'expectPrice',
      key: 'expectPrice',
      width: 160,
      visible: true,
      render: (text: number) => Number(text).toFixed(2),
    },
    {
      title: '预估总价（元）',
      dataIndex: 'expectSum',
      key: 'expectSum',
      width: 160,
      visible: true,
      render: (text: number) => {
        return Number(text).toFixed(2);
      },
    },
    // TODO: 梁豪杰 待确认后删除
    // {
    //     title: '是否通过需求论证',
    //     dataIndex: 'needProofFlag',
    //     key: 'needProofFlag',
    //     width: 200,
    //     visible: true,
    //     render: (text: string) => renderOverflowText(text),
    // },
    // {
    //     title: '需求论证意见',
    //     dataIndex: 'needProofURL',
    //     key: 'needProofURL',
    //     width: 200,
    //     visible: true,
    //     render: (text: string) => renderOverflowText(text, true),
    // },
    // {
    //     title: '是否通过进口专业人员论证',
    //     dataIndex: 'needImportFlag',
    //     key: 'needImportFlag',
    //     width: 260,
    //     visible: true,
    //     render: (text: string) => renderOverflowText(text),
    // },
    // {
    //     title: '进口专业人员论证意见',
    //     dataIndex: 'needImportURL',
    //     key: 'needImportURL',
    //     width: 220,
    //     visible: true,
    //     render: (text: string) => renderOverflowText(text, true),
    // },
    // {
    //     title: '是否通过单一来源论证',
    //     dataIndex: 'needSingleFlag',
    //     key: 'needSingleFlag',
    //     width: 220,
    //     visible: true,
    //     render: (text: string) => renderOverflowText(text),
    // },
    // {
    //     title: '单一来源论证意见',
    //     dataIndex: 'needSingleURL',
    //     key: 'needSingleURL',
    //     width: 200,
    //     visible: true,
    //     render: (text: string) => renderOverflowText(text, true),
    // },
    {
      title: (
        <Button
          type={'link'}
          size={'small'}
          icon={<PlusCircleOutlined />}
          className="manageBtntTxtStyle"
          onClick={() => {
            changeTargetInfo({
              packInfo: {},
              isShow: true,
            });
          }}
        >
          选择
        </Button>
      ),
      dataIndex: 'operation',
      key: 'operation',
      width: 90,
      visible: true,
      unDrag: true,
      align: 'center',
      fixed: 'right',
      render: (_: string, record: Record<string, any>, index: number) => {
        return (
          <Button
            danger
            type={'link'}
            size={'small'}
            className="manageBtntTxtStyle"
            onClick={() => onRemove(record.id, index, record.fullName ?? '标一')}
            icon={<DeleteOutlined />}
          >
            删除
          </Button>
        );
      },
    },
  ];

  if (finallyType == 'view') {
    colums = colums?.filter((item) => item?.key != 'operation');
  }

  return colums;
};

/**
 * @getApplyPackTableColumns 采购申请第三步表格配置
 * @param dataSource 表格数据
 * @param jumpSource 跳转寻源
 * @returns
 */
export const getApplyPackTableColumns = ({
  dataSource,
  jumpSource,
}: {
  dataSource: Record<string, any>[];
  jumpSource: (record: Record<string, any>) => void;
}): Record<string, any>[] => {
  return [
    {
      title: '包号',
      dataIndex: 'packCode',
      key: 'packCode',
      width: 80,
      visible: true,
      render: (text: string, record: Record<string, any>, index: number) => {
        return mergeCells(dataSource, 'packCode', text, record, index);
      },
    },
    {
      title: '包名称',
      dataIndex: 'packName',
      key: 'packName',
      width: 200,
      visible: true,
      render: (text: string, record: Record<string, any>, index: number) => {
        return ColumnsRenderFun({
          text,
          // textStyle: { color: '#1890ff', cursor: 'pointer' },
          // event: jumpSource
        });
        // const cell = mergeCells(dataSource, 'packName', text, record, index);
        // const param = {
        //   children: (
        //     <span onClick={() => jumpSource(record)} style={{ color: '#1890ff', cursor: 'pointer' }}>
        //       {cell?.children}
        //     </span>
        //   ),
        //   props: cell?.props,
        // };
        // return param;
      },
    },
    {
      title: '预算金额（元）',
      dataIndex: 'expectSum',
      key: 'expectSum',
      width: 150,
      visible: true,
      render: (text: string) => {
        return Number(text).toFixed(2);
      },
    },
    {
      title: '拟采用采购方式',
      dataIndex: 'procureRealName',
      key: 'procureRealName',
      width: 150,
      visible: true,
      render: (text: string, record: Record<string, any>, index: number) => {
        return mergeCells(dataSource, ['packName', 'procureRealName'], text, record, index);
      },
    },
  ];
};

/**
 * @getMaterialTableColumns 步骤条第二步 弹窗中标的列表配置
 * @param onEditTable 表格编辑事件
 * @returns
 */
export const getMaterialTableColumns = ({ onEditTable }: any): Record<string, any>[] => {
  return [
    {
      title: '标的名称',
      dataIndex: 'fullName',
      key: 'fullName',
      width: 130,
      visible: true,
      fixed: 'left',
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    // {
    //   title: '计划名称',
    //   dataIndex: 'planName',
    //   key: 'planName',
    //   width: 200,
    //   visible: true,
    //   // fixed: 'left',
    //   render: (text: string) => renderOverflowText(text, true),
    // },
    {
      title: '标的类型',
      dataIndex: 'supplyTypeName',
      key: 'supplyTypeName',
      width: 140,
      visible: true,
      render: (text: string, record) => DictionaryRenderFun({ text }),
    },
    {
      title: '计划数量（单位）',
      dataIndex: 'expectNum',
      key: 'expectNum',
      width: 180,
      visible: true,
      render: (text: string, record: Record<string, any>) => {
        return (
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
            <span>{text}</span>
            <span style={{ margin: '0 5px' }}>{record.unitBusiness ?? '个'}</span>
          </div>
        );
      },
    },
    {
      title: (
        <>
          <MyIcon type="icon-edit1" style={{ marginRight: '.5rem' }} />
          采购数量（单位）
        </>
      ),
      dataIndex: 'inpNum',
      key: 'inpNum',
      width: 200,
      fixed: 'right',
      render: (text: number | string, record: Record<string, any>) => {
        const MaxNum: number = Number(record?.prepareNum || 0) - Number(record?.lockNum || 0);
        if (MaxNum <= 0) {
          return <div style={{ textAlign: 'center' }}>暂无</div>;
        } else {
          let dw: any = null;
          if (record?.unitBusinessName) {
            const { colorText, color } = getColorTextLoadMap(text);
            dw = <span style={{ margin: '0 5px', color }}>{colorText}</span>;
          } else {
            dw = <span style={{ margin: '0 5px' }}>个</span>;
          }

          return (
            <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
              <InputNumber
                bordered={false}
                value={text}
                style={{ width: '100%' }}
                onChange={(value: any) => {
                  onEditTable({
                    value: value,
                    key: 'prepareNum',
                    type: 'InputNumber',
                    record: record,
                  });
                }}
                min={1}
                max={MaxNum}
                disabled={text == 0}
              />
              {dw}
            </div>
          );
        }
      },
    },
    // {
    //   title: '规格要求（参数）',
    //   dataIndex: 'needParams',
    //   key: 'needParams',
    //   width: 180,
    //   visible: true,
    //   render: (text: string) => ColumnsRenderFun({ text }),
    // },
    // {
    //   title: '采购类别',
    //   dataIndex: 'supplyKindName',
    //   key: 'supplyKindName',
    //   width: 140,
    //   visible: true,
    //   render: (text: string) => DictionaryRenderFun({ text }),
    // },
    // {
    //   title: '已采数量（单位）',
    //   dataIndex: 'completeNum',
    //   key: 'completeNum',
    //   width: 170,
    //   visible: true,
    //   render: (text: string, record: Record<string, any>) => {
    //     return (
    //       <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
    //         <span>{text}</span>
    //         <span style={{ margin: '0 5px' }}>{record.unitBusiness ?? '个'}</span>
    //       </div>
    //     );
    //   },
    // },
    {
      title: '规格型号（参数）',
      dataIndex: 'needParams',
      key: 'needParams',
      width: 180,
      visible: true,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '预估单价（元）',
      dataIndex: 'expectPrice',
      key: 'expectPrice',
      width: 160,
      visible: true,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    },
    // {
    //   title: '预估总价（元）',
    //   dataIndex: 'expectSum',
    //   key: 'expectSum',
    //   width: 160,
    //   visible: true,
    //   render: (text: string) => ColumnsRenderFun({ text, isTooltip: false }),
    // },
    // {
    //     title: '是否通过需求论证',
    //     dataIndex: 'needProofFlag',
    //     key: 'needProofFlag',
    //     width: 200,
    //     visible: true,
    //     render: (text: string) => renderOverflowText(text),
    // },
    // {
    //     title: '需求论证意见',
    //     dataIndex: 'needProofURL',
    //     key: 'needProofURL',
    //     width: 200,
    //     visible: true,
    //     render: (text: string) => renderOverflowText(text, true),
    // },
    // {
    //     title: '是否通过进口专业人员论证',
    //     dataIndex: 'needImportFlag',
    //     key: 'needImportFlag',
    //     width: 260,
    //     visible: true,
    //     render: (text: string) => renderOverflowText(text),
    // },
    // {
    //     title: '进口专业人员论证意见',
    //     dataIndex: 'needImportURL',
    //     key: 'needImportURL',
    //     width: 220,
    //     visible: true,
    //     render: (text: string) => renderOverflowText(text, true),
    // },
    // {
    //     title: '是否通过单一来源论证',
    //     dataIndex: 'needSingleFlag',
    //     key: 'needSingleFlag',
    //     width: 220,
    //     visible: true,
    //     render: (text: string) => renderOverflowText(text),
    // },
    // {
    //     title: '单一来源论证意见',
    //     dataIndex: 'needSingleURL',
    //     key: 'needSingleURL',
    //     width: 200,
    //     visible: true,
    //     render: (text: string) => renderOverflowText(text, true),
    // },
  ];
};

/**
 * @getProjectTableColumns 步骤条第一步 项目弹窗配置
 */
export const getProjectTableColumns = (): Record<string, any>[] => {
  return [
    // {
    //     title: '编号',
    //     dataIndex: 'id',
    //     key: 'id',
    //     visible: false,
    //     width: 150,
    // },
    {
      title: '项目编号',
      dataIndex: 'projectCode',
      key: 'projectCode',
      visible: true,
      width: 140,
      sorter: (a: any, b: any) => getSorter(a?.projectCode, b?.projectCode),
    },
    {
      title: '项目名称',
      dataIndex: 'projectName',
      key: 'projectName',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => getSorter(a?.projectName, b?.projectName),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '项目费用（元）',
      dataIndex: 'projectSum',
      key: 'projectSum',
      visible: true,
      width: 120,
      sorter: (a: any, b: any) => getSorter(a?.projectSum, b?.projectSum),
      render: (text: string) => formatDecimal(text),
    },
    {
      title: '项目描述',
      dataIndex: 'projectIntro',
      key: 'projectIntro',
      visible: true,
      width: 230,
      sorter: (a: any, b: any) => getSorter(a?.projectIntro, b?.projectIntro),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
  ];
};
